﻿using System;
using System.Windows.Forms;
using MySqlConnector;
using System.Security.Cryptography;
using System.Text;

namespace Front._25112401
{
    public partial class Login : Form
    {
        private string connectionString = "Server=localhost;Database=book;Uid=root;Pwd=root;";

        public Login()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string username = textBox1.Text.Trim();
            string password = textBox2.Text;

            if (string.IsNullOrEmpty(username))
            {
                MessageBox.Show("请输入管理员账号！");
                textBox1.Focus();
                return;
            }

            if (string.IsNullOrEmpty(password))
            {
                MessageBox.Show("请输入密码！");
                textBox2.Focus();
                return;
            }

            // 临时使用固定账号测试
            if (username == "admin_super01" && password == "123456")
            {
                MessageBox.Show("登录成功！");
        
                // 创建主菜单窗体实例
                Menu mainMenu = new Menu();
        
                // 显示主菜单窗体
                mainMenu.Show();
        
                // 隐藏登录窗体（不是关闭）
                this.Hide();
            }
            else
            {
                MessageBox.Show("账号或密码错误！");
            }
        }

        private bool CheckLogin(string username, string password)
        {
            try
            {
                // 对输入的明文密码进行MD5加密
                string md5Password = CalculateMD5Hash(password);
                
                using (MySqlConnection conn = new MySqlConnection(connectionString))
                {
                    conn.Open();
                    string sql = "SELECT COUNT(*) FROM admin_info WHERE admin_account = @username AND admin_password = @password AND work_status = 1";
                    
                    using (MySqlCommand cmd = new MySqlCommand(sql, conn))
                    {
                        cmd.Parameters.AddWithValue("@username", username);
                        cmd.Parameters.AddWithValue("@password", md5Password); // 使用MD5加密后的密码
                        
                        int count = Convert.ToInt32(cmd.ExecuteScalar());
                        return count > 0;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"数据库错误: {ex.Message}");
                return false;
            }
        }

        // MD5加密方法
        private string CalculateMD5Hash(string input)
        {
            using (MD5 md5 = MD5.Create())
            {
                byte[] inputBytes = Encoding.UTF8.GetBytes(input);
                byte[] hashBytes = md5.ComputeHash(inputBytes);
                
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < hashBytes.Length; i++)
                {
                    sb.Append(hashBytes[i].ToString("x2")); // x2表示十六进制格式
                }
                return sb.ToString();
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            // 留空
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            // 留空
        }
    }
} 